{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "b'{\"code\":0,\"data\":{\"fileList\":[{\"label\":0,\"labels\":[0,1,11,2,3],\"name\":\"http://m.qpic.cn/psb?/V140Qfy60MFs14/LLGdjUvkuPxRnB79l.VQ*Iz3wZxX3U73lhSKTslGh2E!/b/dPIAAAAAAAAA\\\\u0026rf=viewer_311\",\"rate\":0.4857643246650696,\"rates\":[0.4857643246650696,0.4590115249156952,0.044574495404958725,0.010032614693045616,0.000231839221669361],\"review\":true,\"tag\":\"Using url\"}],\"reviewCount\":1,\"topNStatistic\":[{\"count\":1,\"label\":0}]},\"desc\":\"success\",\"sid\":\"tup00024523@dx41a312ecce9e1aba00\"}'\n"
     ]
    }
   ],
   "source": [
    "# -*- coding: utf-8 -*-\n",
    "import requests\n",
    "import time\n",
    "import hashlib\n",
    "import base64\n",
    "\n",
    "# 人脸特征分析年龄webapi接口地址\n",
    "URL = \"http://tupapi.xfyun.cn/v1/age\"\n",
    "# 应用ID  (必须为webapi类型应用，并人脸特征分析服务，参考帖子如何创建一个webapi应用：http://bbs.xfyun.cn/forum.php?mod=viewthread&tid=36481)\n",
    "APPID = \"5f8eda71\"\n",
    "# 接口密钥(webapi类型应用开通人脸特征分析服务后，控制台--我的应用---人脸特征分析---服务的apikey)\n",
    "API_KEY = \"d3942ad85abd612e32af6810f3ef9b8d\"\n",
    "ImageName = \"gaogao\"\n",
    "ImageUrl = \"http://m.qpic.cn/psb?/V140Qfy60MFs14/LLGdjUvkuPxRnB79l.VQ*Iz3wZxX3U73lhSKTslGh2E!/b/dPIAAAAAAAAA&rf=viewer_311\"\n",
    "# 图片数据可以通过两种方式上传，第一种在请求头设置image_url参数，第二种将图片二进制数据写入请求体中。若同时设置，以第一种为准。\n",
    "# 此demo使用第一种方式进行上传图片地址，如果想使用第二种方式，将图片二进制数据写入请求体即可。\n",
    "# FilePath = r\"C:\\Users\\Admin\\Desktop\\1539656523.png\"\n",
    "\n",
    "\n",
    "def getHeader(image_name, image_url=None):\n",
    "    curTime = str(int(time.time()))\n",
    "    param = \"{\\\"image_name\\\":\\\"\" + image_name + \"\\\",\\\"image_url\\\":\\\"\" + image_url + \"\\\"}\"\n",
    "    paramBase64 = base64.b64encode(param.encode('utf-8'))\n",
    "    tmp = str(paramBase64, 'utf-8')\n",
    "\n",
    "    m2 = hashlib.md5()\n",
    "    m2.update((API_KEY + curTime + tmp).encode('utf-8'))\n",
    "    checkSum = m2.hexdigest()\n",
    "\n",
    "    header = {\n",
    "        'X-CurTime': curTime,\n",
    "        'X-Param': paramBase64,\n",
    "        'X-Appid': APPID,\n",
    "        'X-CheckSum': checkSum,\n",
    "    }\n",
    "    return header\n",
    "\n",
    "\n",
    "# def getBody(filePath):\n",
    "#     binfile = open(filePath, 'rb')\n",
    "#     data = binfile.read()\n",
    "#     return data\n",
    "\n",
    "\n",
    "r = requests.post(URL, headers=getHeader(ImageName, ImageUrl))\n",
    "print(r.content)\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.3"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
